package com.woniu.humanresource.service.impl;

import com.woniu.humanresource.dao.AchievementDao;
import com.woniu.humanresource.dao.UserDao;
import com.woniu.humanresource.entity.*;
import com.woniu.humanresource.service.AchievementService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class AchievementServiceImpl implements AchievementService {
    @Autowired
    AchievementDao achievementDao;

    @Autowired
    UserDao userDao;

    @Override
    public List<Achievement> selectAchievement(int pageIndex ,int pnum,int dapartid) {
        List<Achievement> achievements = achievementDao.selectAchievement(pageIndex,pnum,dapartid);
        return achievements;
    }

    @Override
    public int selectAllEmp(int pnum,int dapartid) {
        return achievementDao.selectAllEmp(pnum,dapartid);
    }

    @Override
    public Achievement selectAllAchievementContext() {
        Achievement achievement = new Achievement();
        achievement.setAchievementEfficiencies(achievementDao.selectAllEfficienciesContext());
        achievement.setAchievementBehaviors(achievementDao.selectAllBehaviorContext());
        return achievement;
    }

    @Override
    public void insertAchievement(Achievement achievement, int id, String uuname) {
        //判断achievement 表中有没有id的数据 有就更改
        try{
            int i = achievementDao.selectId(id);
            upDateAchievement(achievement,id,uuname);
            return;
        } catch (Exception e) {
            //1.查eid
            int eid = userDao.selectEid(uuname);
            //2.插入achievement 表 同时获取到主键
            InsertAchievement insertAchievement = new InsertAchievement(1,id,eid);
            achievementDao.insertAchievement(insertAchievement);
            int aid = insertAchievement.getKey();
            //3.遍历Achievement 的主键 在插入achievement_efficiency中
            String accontext;
            List<AchievementEfficiency> achievementEfficiencies = achievement.getAchievementEfficiencies();
            for(AchievementEfficiency achievementEfficiency :achievementEfficiencies){
                accontext = achievementEfficiency.getAccontext();
                int aeaid = achievementDao.selectEfficiencyId(accontext);
                //插入数据
                achievementDao.insertEfficiency(aeaid,achievementEfficiency.getScores(), aid);
            }
            List<AchievementBehavior> achievementBehaviors = achievement.getAchievementBehaviors();
            for(AchievementBehavior achievementBehavior : achievementBehaviors ){
                accontext = achievementBehavior.getAccontext();
                int abaid = achievementDao.selectBehavioridId(accontext);
                //插入数据
                achievementDao.insertBehavior(abaid,achievementBehavior.getScores(),aid);
            }

        }




    }


    public void upDateAchievement(Achievement achievement, int id, String uuname) {
        //通过 id 把 aid查出来
        int aid = achievementDao.selectId(id);
        //查eid
        //int eid = userDao.selectEid(uuname);

        //遍历更改
        String accontext;
        List<AchievementEfficiency> achievementEfficiencies = achievement.getAchievementEfficiencies();
        for(AchievementEfficiency achievementEfficiency :achievementEfficiencies){
            accontext = achievementEfficiency.getAccontext();
            int aeaid = achievementDao.selectEfficiencyId(accontext);
            //更新数据
            achievementDao.updateEfficiency(aeaid,achievementEfficiency.getScores(),aid);
        }

        List<AchievementBehavior> achievementBehaviors = achievement.getAchievementBehaviors();
        for(AchievementBehavior achievementBehavior :achievementBehaviors){
            accontext = achievementBehavior.getAccontext();
            int abaid = achievementDao.selectBehavioridId(accontext);
            //更新数据
            achievementDao.updateBehavior(abaid,achievementBehavior.getScores(),aid);
        }

    }



}
